package com.example.springboot.common.Result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import static com.example.springboot.common.constants.Constants.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    private String code;
    private String msg;
    private T data;

    public Result(T data) {
        this.data = data;
    }

    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(CODE_SUCCESS);
        result.setMsg(SUCCESS);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>(data);
        result.setCode(CODE_SUCCESS);
        result.setMsg(SUCCESS);
        return result;
    }

    public static <T> Result<T> success(String message, T data) {
        Result<T> result = new Result<>(data);
        result.setCode(CODE_SUCCESS);
        result.setMsg(message);
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result result = new Result();
        result.setCode(COED_ERROR);
        result.setMsg(msg);
        result.setData(ERROR);
        return result;
    }

    public static <T> Result<T> error(String code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(ERROR);
        return result;
    }

    public static <T> Result<T> ifResult(int rows) {
        return rows > 0 ? Result.success() : Result.error(ERROR);
    }
}
